<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PLC显示测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .result-item {
            display: flex;
            justify-content: space-between;
            padding: 10px;
            margin: 5px 0;
            background: #f8f9fa;
            border-radius: 4px;
        }
        .result-name {
            font-weight: 500;
            color: #555;
        }
        .result-value {
            font-weight: bold;
            color: #4CAF50;
        }
        .test-area {
            margin: 20px 0;
            padding: 15px;
            background: #e8f5e8;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>PLC显示测试</h1>
        
        <div class="test-area">
            <h2>模拟PLC读取结果</h2>
            <p>这里显示从PLC读取的数据:</p>
            
            <div id="results-container">
                <!-- 结果将通过JavaScript动态插入 -->
            </div>
        </div>
        
        <div class="test-area">
            <h2>测试数据</h2>
            <button onclick="testDisplay()">测试显示</button>
            <button onclick="testWithNullValues()">测试包含null值</button>
            <button onclick="testWithUndefinedValues()">测试包含undefined值</button>
        </div>
    </div>

    <script>
        // 模拟PLC读取结果
        function displayResults(data) {
            const container = document.getElementById('results-container');
            container.innerHTML = '';
            
            if (Object.keys(data).length === 0) {
                container.innerHTML = '<p>暂无数据</p>';
                return;
            }
            
            for (const [name, value] of Object.entries(data)) {
                const itemDiv = document.createElement('div');
                itemDiv.className = 'result-item';
                
                const nameSpan = document.createElement('span');
                nameSpan.className = 'result-name';
                nameSpan.textContent = name + ':';
                nameSpan.title = name;
                
                const valueSpan = document.createElement('span');
                valueSpan.className = 'result-value';
                // 处理值显示逻辑
                const displayValue = (value !== undefined && value !== null) ? value : '-';
                valueSpan.textContent = displayValue;
                valueSpan.title = value;
                
                itemDiv.appendChild(nameSpan);
                itemDiv.appendChild(valueSpan);
                container.appendChild(itemDiv);
            }
        }
        
        // 测试显示功能
        function testDisplay() {
            const testData = {
                'DB1,W0': 44,
                'DB1,W2': 3,
                'DB2,X0.0': true,
                'DB2,X0.1': true
            };
            displayResults(testData);
        }
        
        // 测试包含null值
        function testWithNullValues() {
            const testData = {
                'DB1,W0': 44,
                'DB1,W2': null,
                'DB2,X0.0': true,
                'DB2,X0.1': null
            };
            displayResults(testData);
        }
        
        // 测试包含undefined值
        function testWithUndefinedValues() {
            const testData = {
                'DB1,W0': 44,
                'DB1,W2': undefined,
                'DB2,X0.0': true,
                'DB2,X0.1': undefined
            };
            displayResults(testData);
        }
        
        // 页面加载完成后显示测试数据
        document.addEventListener('DOMContentLoaded', function() {
            testDisplay();
        });
    </script>
</body>
</html>